//1. 安装
//2. 导入
const mongoose = require("mongoose");

//3. 连接 mongodb 数据库
mongoose.connect("mongodb://127.0.0.1:27017/h5220707");

//4. 设置回调  on  when 当...时候
mongoose.connection.on("open", () => {
  //5. 声明文档结构对象 
  const MovieSchema = new mongoose.Schema({
    name: String,
    actors: Array,
    releaseYear: Number,
  });

  //6. 创建文档模型对象
  const MovieModel = mongoose.model("movie", MovieSchema);

  //7. 文档操作
  //批量获取  如果不加条件, 就是获取所有   
  // MovieModel.find().select({name: 1, _id: 0}).exec((err, data) => {
  //   if(err) throw err;
  //   console.log(data);
  //   mongoose.connection.close();
  // })

  //排序
  // MovieModel.find().sort({releaseYear: -1}).exec((err, data) => {
  //   if(err) throw err;
  //   console.log(data);
  //   mongoose.connection.close();
  // })

  //数据截取
  MovieModel.find().skip(4).limit(5).select({name: 1}).sort({releaseYear: 1}).exec((err, data) => {
    if(err) throw err;
    console.log(data);
    mongoose.connection.close();
  })

})